package main;

import dao.Dao;
import model.Admin;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 * Created by JFormDesigner on Mon Jun 05 20:04:03 CST 2023
 */



/**
 * @author YangC
 */
public class Login extends JFrame {
    public Login() {
        initComponents();
    }

    public static void main(String[] args) {
        try {
            for(UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
            {
                if (info.getName().equals("Nimbus"))
                {
                    try {
                        UIManager.setLookAndFeel(info.getClassName());
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }

            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        Login admin = new Login();
        admin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        admin.setLocationRelativeTo(null);
        admin.setVisible(true);
    }

    private void close(ActionEvent e) {
        // TODO add your code here
        System.exit(0);
    }

    private void button3(ActionEvent e) {
        // TODO add your code here
        Admin po = Dao.check(textField3.getText(),passwordField3.getText());

        if(po == null)
        {
            JOptionPane.showMessageDialog(null,"登录失败！");
        }
        else
        {
            new MainFm().setVisible(true);
            dispose();
        }
    }

    private void passwordField3KeyTyped(KeyEvent e) {
        // TODO add your code here
        if(e.getKeyChar() == '\n')
        {
            button3.doClick();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button2 = new JButton();
        label4 = new JLabel();
        label5 = new JLabel();
        button3 = new JButton();
        textField3 = new JTextField();
        passwordField3 = new JPasswordField();
        label1 = new JLabel();

        //======== this ========
        setTitle("\u5e93\u5b58\u7ba1\u7406\u7cfb\u7edf");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- button2 ----
        button2.setText("\u53d6\u6d88");
        button2.addActionListener(e -> close(e));
        contentPane.add(button2);
        button2.setBounds(253, 224, 76, 28);

        //---- label4 ----
        label4.setText("\u8d26\u6237\uff1a");
        contentPane.add(label4);
        label4.setBounds(106, 129, label4.getPreferredSize().width, 21);

        //---- label5 ----
        label5.setText("\u5bc6\u7801\uff1a");
        contentPane.add(label5);
        label5.setBounds(106, 164, label5.getPreferredSize().width, 23);

        //---- button3 ----
        button3.setText("\u767b\u5f55");
        button3.setAutoscrolls(true);
        button3.addActionListener(e -> button3(e));
        contentPane.add(button3);
        button3.setBounds(105, 224, 76, 26);
        contentPane.add(textField3);
        textField3.setBounds(147, 123, 168, 30);

        //---- passwordField3 ----
        passwordField3.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                passwordField3KeyTyped(e);
            }
        });
        contentPane.add(passwordField3);
        passwordField3.setBounds(147, 161, 169, 31);

        //---- label1 ----
        label1.setIcon(new ImageIcon(getClass().getResource("/ref/\u767b\u5f55.png")));
        contentPane.add(label1);
        label1.setBounds(-20, -75, 565, 490);

        contentPane.setPreferredSize(new Dimension(430, 305));
        setSize(430, 305);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button2;
    private JLabel label4;
    private JLabel label5;
    private JButton button3;
    private JTextField textField3;
    private JPasswordField passwordField3;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
